package com.jx.mlxg.utils;

<<<<<<< HEAD

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
=======
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
>>>>>>> eb72a42842a7e02f996c4f2d21dc4051aa14ecf1

@Component
public class JwtUtil {

<<<<<<< HEAD
    private static final String SECRET_KEY = "10010";



    private static final String JWT_SECRET_KEY = "10010"; // 应与配置一致

    public static String genToken(String openId) {
        return JWT.create()
                .withSubject(openId) // 设置openId为subject
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7)) // 7天有效期
                .sign(Algorithm.HMAC256(JWT_SECRET_KEY));
    }

    // 接收token,验证token,并返回业务数据
    public static Map<String, Object> parseToken(String token) {
        Map<String, Object> claims =
                JWT.require(Algorithm.HMAC256(JWT_SECRET_KEY)).build().verify(token).getClaim("claims").asMap();
        // 对claims进行遍历，将整数转换为长整数
        for (Map.Entry<String, Object> entry : claims.entrySet()) {
            if (entry.getValue() instanceof Integer) {
                entry.setValue(((Integer)entry.getValue()).longValue());
            }
        }
        return claims;
    }

=======
    private static final String SECRET_KEY = "10086";

    public String generateToken(String username) {
        return Jwts.builder()
                .setSubject(username)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) // 10 hours
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
    }

    public boolean validateToken(String token) {
        try {
            Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token);
            return true;
        } catch (JwtException e) {
            return false;
        }
    }

    public String getUsernameFromToken(String token) {
        return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody().getSubject();
    }
>>>>>>> eb72a42842a7e02f996c4f2d21dc4051aa14ecf1
}
